<?php
/**
 * 物业
 * @author Administrator
 *
 */
class PropertyAction extends Action {
    public function index(){
    	$this->display();
    }
    
    public function savePaymentAddress() {
    	$addr   =   D('PaymentAddress');

    	if($addr->create()) {
    		$addr->mobile_no = $_SESSION["user"]["mobile_no"];
    		$addr->name = $_SESSION["user"]["name"];
    		$result =   $addr->add();
    		if($result) {
    			$this->success('保存成功！2' + $_SESSION["user"]["mobile_no"]);
    		}else{
    			$this->error('保存出错！');
    		}
    	}else{
    		$this->error($addr->getError());
    	}
    }
    
    /**
     * 获取缴费信息
     */
    public function wy2() {
    	// 获取地址信息
    	$mobile_no= $_SESSION["user"]["mobile_no"];
    	
    	$Data = M('PaymentAddress'); // 实例化Data数据模型
    	$this->address = $Data->where("mobile_no='{$mobile_no}'")->select();
    	
    	
    	$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
    	
    	$sql = "select count(id) total_month, sum(total_fee) total_fee from ic_property_fee where is_payment=0 and mobile_no ='{$mobile_no}'";
    	$result = $Model->query($sql);
    	$total_month = 0;
    	$total_fee = 0.0;
    	
    	if ($result==false) {
    		$this->error($sql);
    	} 
    	
    	$total_month = $result[0]["total_month"];
    	$total_fee = $result[0]["total_fee"];
    	//dump($total_month);
    	$this->assign('sql',$result);
    	$this->assign('total_month',$total_month);
    	$this->assign('total_fee',$total_fee);
    	$this->display('wy2');
    }
    
    /**
     * 查询缴费明细
     */
    public function wy3() {
    	// 获取地址信息
    	$mobile_no= $_SESSION["user"]["mobile_no"];
    	 
    	$Data = M('PaymentAddress'); // 实例化Data数据模型
    	$this->address = $Data->where("mobile_no='{$mobile_no}'")->select();
    	
    	$Data = M('PropertyFee'); // 实例化Data数据模型
    	$this->feeList = $Data->where("mobile_no='{$mobile_no}' and is_payment=0 ")->select();
    	$this->display('wy3');
    }
    
    public function wy4() {
    	// 获取地址信息
    	$feeId = $_POST['feeId'];
    	$feeIdStr = join(",", $feeId);
    	// dump($feeIdStr);
    	// 获取地址信息
    	$mobile_no= $_SESSION["user"]["mobile_no"];
    	
    	
    	$Data = M('PaymentAddress'); // 实例化Data数据模型
    	$this->address = $Data->where("mobile_no='{$mobile_no}'")->select();
    	 
    	$Data = M('PropertyFee'); // 实例化Data数据模型
    	$this->feeList = $Data->where("mobile_no='{$mobile_no}' and is_payment=0 and id in ({$feeIdStr})")->select();
    	
    	$totalAmount = 0.0;
    	foreach($this->feeList as $f){
    		$totalAmount += $f["total_fee"];
    	}
    	$this->totalAmount=$totalAmount;
    	
    	// 创建订单
    	$paymentInfo   =   D('PaymentInfo');
    	$paymentInfo->mobile_no = $mobile_no;
    	$paymentInfo->name = $_SESSION["user"]["name"];
    	$paymentInfo->address = "{$this->address[0]['community_no']}{$this->address[0]['building_no']}{$this->address[0]['room_no']}";
    	$paymentInfo->totalAmount  = $totalAmount;
    	$paymentInfo->status  = '0';
    	$paymentInfo->create_time = date();
    	
    	$result =   $paymentInfo->add();
    	 
    	$this->display('wy4');
    }
}